home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CU Amiga Super CD-ROM 23
/
CU Amiga - Super CD-ROM 23 (June 1998).iso
/
CUCD
/
Magazine
/
C_Tutorial
/
Part-11
/
req1.bak
/
loadsave.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-04-05
|
5KB
|
172 lines
#include "loadsave.h"
#include "bitmap.h"
#include "drawwin.h"
#include "gui.h"
#include "main.h"
#include "iff.h"
#include<libraries/asl.h>
#include<intuition/intuition.h>
#include<workbench/workbench.h>
#include<string.h>
#include<stdio.h>
#include<clib/asl_protos.h>
#include<clib/dos_protos.h>
#include<clib/graphics_protos.h>
#include<clib/icon_protos.h>
/* Global handles for our requesters */
static struct FileRequester* loadreq = NULL;
static struct FileRequester* savereq = NULL;
/* Open an ASL load file requester */
int load()
{
/* Allocate the requester if we haven't already */
if(loadreq == NULL)
loadreq = (struct FileRequester*)
AllocAslRequestTags(ASL_FileRequest,
ASLFR_TitleText, "Load File",
ASLFR_Flags1, FRF_DOPATTERNS,
ASLFR_InitialPattern, "#?.iff",
TAG_DONE);
if(loadreq)
{
struct Window* win = getDrawWin();
if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
{
char filename[MAXFILENAME];
/* Create complete filename from ASL's dir and file */
strcpy(filename, loadreq->rf_Dir);
if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
return loadfile(filename);
else
printf("Error: could not make filename\n");
}
/* else: requester was cancelled */
}
else
printf("Error: could not allocate ASL (load) file request\n");
/* If we get this far then there was no fatal error */
return TRUE;
}
int loadfile(char* filename)
{
/* Record any fatal errors */
int going = TRUE;
IFFL_HANDLE handle;
/* Try to open the IFF file */
if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
{
UWORD colortable[256];
/* Get colour information */
LONG count = IFFL_GetColorTab(handle, colortable);
/* Get display information */
ULONG displayid = IFFL_GetViewModes(handle);
/* Get picture information */
struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
struct Window* win;
/* Try to adjust the screen to fit */
if(bmhd)
{
closeGUI();
/* If this fails, our local win will be NULL */
openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid);
}
if(win = getDrawWin())
{
/* Change screen colours */
LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
/* If we can load the picture, update window's display */
if(IFFL_DecodePic(handle, getBitmap()))
{
CopySBitMap(win->WLayer);
setModified(FALSE);
}
else
printf("Error: could not decode IFF picture\n");
}
else
going = FALSE; /* The only fatal error */
IFFL_CloseIFF(handle);
}
else
printf("Error: could not open IFF file\n");
return going;
}
/* Open an ASL save file requester */
void save()
{
/* Another way of saying "allocate if we haven't already" */
if(savereq ||
(savereq = (struct FileRequester*)
AllocAslRequestTags(ASL_FileRequest,
ASLFR_TitleText, "Save File",
ASLFR_Flags1, FRF_DOPATTERNS | FRF_DOSAVEMODE,
ASLFR_InitialPattern, "#?.iff",
ASLFR_InitialFile, "picture.iff",
TAG_DONE)))
{
struct Window* win = getDrawWin();
if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
{
char filename[MAXFILENAME];
/* Create complete filename from ASL's dir and file */
strcpy(filename, savereq->rf_Dir);
if(AddPart(filename, savereq->rf_File, MAXFILENAME))
{
/* Make sure our bitmap is the same as the display */
SyncSBitMap(win->WLayer);
/* Try saving our bitmap, using the screen's colours */
if(IFFL_SaveBitMap(filename, getBitmap(),
win->WScreen->ViewPort.ColorMap->ColorTable,
IFFL_COMPR_BYTERUN1))
{
/* Write an icon for the file */
struct DiskObject* dobj = GetDefDiskObject(WBPROJECT);
setModified(FALSE);
if(dobj)
{
/* Temporarily change the default tool */
char* dtool = dobj->do_DefaultTool;
dobj->do_DefaultTool = progName();
/* Write out our icon */
PutDiskObject(filename, dobj);
/* Reinstate the default tool */
dobj->do_DefaultTool = dtool;
FreeDiskObject(dobj);
}
}
else
printf("Error: could not write IFF picture\n");
}
else
printf("Error: could not make filename\n");
}
/* else: requester was cancelled */
}
else
printf("Error: could not allocate ASL (save) file request\n");
}
/* Free any requesters that may have been allocated */
void freeReqs()
{
if(loadreq)
{
FreeAslRequest(loadreq);
loadreq = NULL;
}
if(savereq)
{
FreeAslRequest(savereq);
savereq = NULL;
}
}